package cn.blog.dto;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.fasterxml.jackson.annotation.JsonIgnore;

import cn.blog.entity.SystemMenu;
import cn.blog.entity.SystemUser;

public class LoginUser extends SystemUser implements UserDetails {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1772166338342124166L;
	// 登录人员的菜单权限
	private List<SystemMenu> menus;

	private String token;
	/** 登录时间戳 **/
	private Long logintime;
	/** 过期时间戳 **/
	private Long expiretime;

	// 获取它权限重写
	@Override
	@JsonIgnore
	public Collection<? extends GrantedAuthority> getAuthorities() {
		Collection<GrantedAuthority> collection = new HashSet<GrantedAuthority>();
		menus.parallelStream().forEach(m -> {
			if (StringUtils.isNotBlank(m.getPermission())) {
				collection.add(new SimpleGrantedAuthority(m.getPermission()));
			}
		});
		return collection;
	}

	@Override
	@JsonIgnore
	// 是否过期
	public boolean isAccountNonExpired() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	@JsonIgnore
	// 是否被锁定
	public boolean isAccountNonLocked() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	@JsonIgnore
	// 密码是否过期
	public boolean isCredentialsNonExpired() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	@JsonIgnore
	// 账户是否被激活
	public boolean isEnabled() {
		// TODO Auto-generated method stub
		return true;
	}

	public List<SystemMenu> getMenus() {
		return menus;
	}

	public void setMenus(List<SystemMenu> menus) {
		this.menus = menus;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public Long getLogintime() {
		return logintime;
	}

	public void setLogintime(Long logintime) {
		this.logintime = logintime;
	}

	public Long getExpiretime() {
		return expiretime;
	}

	public void setExpiretime(Long expiretime) {
		this.expiretime = expiretime;
	}

}